﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 操作员周预期
    /// </summary>
    public class OperatorWeekExpected
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易周
        /// </summary>
        public int Week { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        public void InitOperatorWeekExpected(int tradeHistoryId, DB.DBManager db)
        {
            List<BenefitHelper.Sys.Operator> operators = db.Operators.ToList();
            foreach (BenefitHelper.Sys.Operator op in operators)
            {
                SetOperatorWeekExpected(op.Id, tradeHistoryId, db);
            }
            db.SaveChanges();
        }

        public void Delete(int year, int month, int week, DB.DBManager db)
        {
            var query = db.OperatorWeekExpected.Where(a => a.Week == week).Where(a => a.Year == year).ToList();
            foreach (OperatorWeekExpected op in query)
            {
                db.OperatorWeekExpected.Remove(op);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 查询交易员周预期
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public OperatorWeekExpected GetOperatorWeekExpected(int operatorid, int week, int year, DB.DBManager db)
        {
            var query = db.OperatorWeekExpected.Where(a => a.OperatorId == operatorid).Where(a => a.Week == week).Where(a => a.Year == year).OrderByDescending(a => a.Id).ToList();
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取操作员周预期(这个方法应该每天去算，现在是从账户周预期里面查到，以后需要优化)
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public void SetOperatorWeekExpected(int operatorid, int tradeHistoryId, DB.DBManager db)
        {
            OperatorWeekExpected change = new OperatorWeekExpected();
            BenefitHelper.Sys.TradeHistory th = db.TradeHistorys.Find(tradeHistoryId);
            change.Week = th.WPdate;
            change.Year = th.YPdate;
            change.Month = th.MPdate;
            change.OperatorId = operatorid;
            List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
            var queryExist = db.OperatorWeekExpected.Where(a => a.OperatorId == operatorid).Where(a => a.Year == th.YPdate).Where(a => a.Week == th.WPdate).ToList();
            if (queryExist.Count > 0)
            {
                if (accounts.Count > 0)
                {
                    foreach (Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountWeekExpected where t.AccountId == account.Id && t.Week == th.WPdate && t.Year == th.YPdate select t;
                        if (query.Count() > 0)
                        {
                            queryExist.First().BillCount += query.First().BillCount;
                            queryExist.First().DayCount += query.First().DayCount;
                            queryExist.First().Free += query.First().Free;
                            queryExist.First().LoseCount += query.First().LoseCount;
                            queryExist.First().OperatorId = operatorid;
                            queryExist.First().Profit += query.First().Profit;
                            queryExist.First().SumLost += query.First().SumLost;
                            queryExist.First().SumWin += query.First().SumWin;
                            queryExist.First().UsedMargin += query.First().UsedMargin;
                            queryExist.First().WinCount += query.First().WinCount;
                        }
                    }
                }
            }
            else
            {
                if (accounts.Count > 0)
                {
                    foreach (Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountWeekExpected where t.AccountId == account.Id && t.Week == th.WPdate && t.Year == th.YPdate select t;
                        if (query.Count() > 0)
                        {
                            change.BillCount += query.First().BillCount;
                            change.DayCount += query.First().DayCount;
                            change.Free += query.First().Free;
                            change.LoseCount += query.First().LoseCount;
                            change.OperatorId = operatorid;
                            change.Profit += query.First().Profit;
                            change.SumLost += query.First().SumLost;
                            change.SumWin += query.First().SumWin;
                            change.UsedMargin += query.First().UsedMargin;
                            change.WinCount += query.First().WinCount;
                        }
                    }
                    db.OperatorWeekExpected.Add(change);
                }

            }


        }
    }
}
